//
//  PYQListImageCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2020/12/29.
//  Copyright © 2020 叁拾叁. All rights reserved.
//

import UIKit
import SnapKit
import Kingfisher
import SwiftDate

class PYQListImageCell: SYBaseCell {

    let itemWidth: Double = 37.5
    let dayLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 21.autoBoldFontSize()
        
        return v
    }()
    
    let monthLb: UILabel = {
        let v: UILabel = UILabel()
        v.font = autoFontSize(12)
        v.textColor = UIColor(hex: 0x333333)
        
        return v
    }()
    
    let imageContent: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0xF7F7F7)
        return v
    }()
    
    let contentLb: UILabel = {
        let v: UILabel = UILabel()
        v.font = 16.autoFontSize()
        v.textColor = UIColor(hex: 0x1E1E1E)
        
        v.layer.cornerRadius = 2
        v.numberOfLines = 0
        return v
    }()
    
    let numLb: UILabel = {
        let v: UILabel = UILabel()
        v.font = autoFontSize(12)
        v.textColor = UIColor(hex: 0x666666)
        return v
    }()
    
    let play: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "play")
        v.isUserInteractionEnabled = true
        v.isHidden = true
        return v
    }()
    
    var imageViews: [UIImageView] = [UIImageView]()
    
    
    public func configure(urls: [String], model: NSVoListModel, isVideo: Bool) {
        setUpImages(urls)
        if let d = model.updateDate {
            let date = DateInRegion(d)
            dayLb.text = "\(date?.day ?? 0)"
            monthLb.text = "\(date?.month ?? 0)月"
        } else if let d = model.createDate {
            let date = DateInRegion(d)
            dayLb.text = "\(date?.day ?? 0)"
            monthLb.text = "\(date?.month ?? 0)月"
        } else {
            dayLb.text = ""
            monthLb.text = ""
        }
        
        contentLb.text = model.content
        play.isHidden = !isVideo
        imageContent.addSubview(play)
        imageContent.bringSubviewToFront(play)
        play.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.height.equalTo(25.autoWidth())
        }
    }
    
    func setUpImages(_ urls:[String]) {
        
        numLb.text = "共\(urls.count)张"
        switch urls.count {
        case 1:
            oneImage(urls)
        case 2:
            twoImage(urls)
        case 3:
            threeImage(urls)
        case 4:
            fourImage(urls)
        default:
            break
        }
    }
    
    func oneImage(_ urls:[String]) {
        let imageView: UIImageView = UIImageView()
        if let r = urls.first {
            imageView.kf.setImage(with: URL(string: r))
            if r.contains("mp4") {
                let videoIcon: UIImageView = UIImageView()
                videoIcon.image = UIImage(named: "")
                imageView.addSubview(videoIcon)
            }
        }
        imageContent.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func twoImage(_ urls:[String]) {
        
        for (i, value) in urls.enumerated() {
            
            let imageView: UIImageView = UIImageView()
            imageView.kf.setImage(with: URL(string: value))
            imageContent.addSubview(imageView)
            if i == 0 {
                imageView.snp.makeConstraints { (make) in
                    make.width.equalTo(itemWidth.autoWidth())
                    make.left.top.bottom.equalToSuperview()
                }
            } else {
                imageView.snp.makeConstraints { (make) in
                    make.width.equalTo(itemWidth.autoWidth())
                    make.right.top.bottom.equalToSuperview()
                }
            }
        }
    }
    
    func threeImage(_ urls:[String]) {
        
        for (i, value) in urls.enumerated() {
            
            let imageView: UIImageView = UIImageView()
            imageView.kf.setImage(with: URL(string: value))
            imageContent.addSubview(imageView)
            
            imageView.snp.makeConstraints { (make) in
                make.width.equalTo(itemWidth.autoWidth())
                switch i {
                case 0:
                    make.left.top.bottom.equalToSuperview()
                case 1:
                    make.height.equalTo(itemWidth.autoWidth())
                    make.right.top.equalToSuperview()
                case 2:
                    make.height.equalTo(itemWidth.autoWidth())
                    make.right.bottom.equalToSuperview()
                default:
                    break
                }
            }
        }
    }
    
    func fourImage(_ urls:[String]) {
        
        for (i, value) in urls.enumerated() {
            
            let imageView: UIImageView = UIImageView()
            imageView.kf.setImage(with: URL(string: value))
            imageContent.addSubview(imageView)
            
            imageView.snp.makeConstraints { (make) in
                make.width.equalTo(itemWidth.autoWidth())
                make.height.equalTo(itemWidth.autoWidth())
                switch i {
                case 0:
                    make.left.top.equalToSuperview()
                case 1:
                    make.right.top.equalToSuperview()
                case 2:
                    make.left.bottom.equalToSuperview()
                case 3:
                    make.right.bottom.equalToSuperview()
                default:
                    break
                }
            }
        }
    }
    
    override func initViews() {
        
        addSubview(dayLb)
        addSubview(monthLb)
        addSubview(imageContent)
        addSubview(contentLb)
        addSubview(numLb)
        
        dayLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(2.5.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(15.5))
            make.width.equalTo(28.0.autoWidth())
            make.height.equalTo(16.0.autoWidth())
        }
        monthLb.snp.makeConstraints { (make) in
            make.bottom.equalTo(dayLb.snp.bottom)
            make.left.equalTo(dayLb.snp.right).offset(autoWidth(3.0))
            make.width.equalTo(25.0.autoWidth())
            make.height.equalTo(11.0.autoWidth())
        }
        imageContent.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalTo(monthLb.snp.right).offset(autoWidth(19.5))
            make.width.equalTo(76.0.autoWidth())
            make.height.equalTo(76.0.autoWidth())
            make.bottom.equalToSuperview().offset(-30.0.autoWidth())
        }
        contentLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalTo(imageContent.snp.right).offset(autoWidth(5.0))
            make.right.equalToSuperview().offset(-17.0.autoWidth())
            make.height.equalTo(56.0.autoWidth())
        }
        numLb.snp.makeConstraints { (make) in
            make.bottom.equalTo(imageContent.snp.bottom)
            make.left.equalTo(imageContent.snp.right).offset(autoWidth(5.5))
            make.width.equalTo(50.0.autoWidth())
            make.height.equalTo(11.5.autoWidth())
        }
       
    }

}
